﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using TransControlSystem.Dao;
using TransControlSystem.Model;
using TransControlSystem.Utils;
using TransControlSystem.ViewModel.Common;
using TransControlSystem.ViewModel.Common.Command;

namespace TransControlSystem.ViewModel
{
    /// <summary>
    /// 登录页面后台逻辑
    /// </summary>
    public class LoginViewModel : ViewModelBase
    {
        private bool toClose = false;
        /// <summary>
        /// 是否要关闭窗口
        /// </summary>
        public bool ToClose
        {
            get
            {
                return toClose;
            }
            set
            {
                toClose = value;
                if (toClose)
                {
                    RaisePropertyChanged("ToClose");
                }
            }
        }
        private User _LoginUser;
        /// <summary>
        /// 登录用户
        /// </summary>
        public User LoginUser
        {
            get { return _LoginUser; }
            set
            {
                _LoginUser = value;
                RaisePropertyChanged("LoginUser");
            }

        }
        /// <summary>
        /// 登录命令
        /// </summary>
        public DelegateCommand LoginCommand { get; set; }
        /// <summary>
        /// 退出程序命令
        /// </summary>
        public DelegateCommand CloseAppCommand { get; set; }

        /// <summary>
        /// 登录构造
        /// </summary>
        public LoginViewModel()
        {
            LoginUser = new User();
            LoginCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(LoginHandler)
            };
            CloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CloseAppHandler)
            };
        }

        /// <summary>
        /// 登录验证处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void LoginHandler(object obj)
        {
            try
            {

                if (LoginUser.Account == null || LoginUser.Account.Trim().Length == 0)
                {
                    WindowManager.ShowWarning("警告", "请输入账号！");
                    return;
                }
                else if (LoginUser.Password == null || LoginUser.Password.Trim().Length == 0)
                {
                    WindowManager.ShowWarning("警告", "请输入密码！");
                    return;
                }
                else if (XyhUtils.IsHasSQLInject(LoginUser.Account))
                {
                    WindowManager.ShowWarning("警告", "账号不可用包含SQL注入关键词！");
                    return;
                }
                else if (XyhUtils.IsHasSQLInject(XyhUtils.Encrypt(LoginUser.Password)))
                {
                    WindowManager.ShowWarning("警告", "密码不可用包含SQL注入关键词！");
                    return;
                }
                else
                {
                    User VerificationUser = new User();
                    VerificationUser.Account = LoginUser.Account;
                    VerificationUser.Password = XyhUtils.Encrypt(LoginUser.Password);
                    string sql = "select * from [User] where Account = @Account and Password = @Password";
                    User DataBaseUser = DapperTools.Query<User>(sql, VerificationUser);
                    if (DataBaseUser == null)
                    {
                        WindowManager.ShowWarning("警告", "账号或密码不正确!");
                        return;
                    }
                    else
                    {
                        //执行登录逻辑
                        LogTool.CurrentUser = DataBaseUser;
                        LogTool.SaveLog("登录", "用户【" + DataBaseUser.UserName + "】登录了系统。");
                        WindowManager.Show("MainWindow", null);
                        Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideLoginWindow"));
                    }
                }
            }
            catch (Exception e)
            {
                LogTool.SaveSysLog(e.Message, "登录异常");
            }
        }
        /// <summary>
        /// 退出程序处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void CloseAppHandler(object obj)
        {
            Environment.Exit(0);
        }
    }
}
